Skip to content

refactor(config): env accessors for secrets, operators, port and snapshot params (#47) - #97

Open
SherlockOmss wants to merge 1 commit into
mainfrom
refactor/47-env-accessors
Open

SherlockOmss wants to merge 1 commit into
mainfrom
refactor/47-env-accessors

Conversation

@SherlockOmss

@SherlockOmss SherlockOmss commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Second PR of SV-014 (#47); the first was #90.

What changes

  • lib/env.ts gains proxySecret, cronSecret, platformOperatorIds and listenPort. lib/tenant.ts, lib/cron-auth.ts, lib/platform-operator.ts and lib/session-lookup.ts read through them instead of process.env.
  • paramsFromEnv leaves app/api/cron/build-snapshots/route.ts and becomes snapshotParamsFromEnv in lib/snapshot-publish.ts.
  • assertStartupConfig also checks PORT and the SNAPSHOT_* numbers, so a wrong value logs Invalid configuration, exiting and exits with status 1.
  • docs/configuration.md says which values are checked at start.

Behavior

Unchanged where it matters: an unset or empty NGINX_PROXY_SECRET or CRON_SECRET still refuses every call, an unset or empty PLATFORM_OPERATOR_USER_IDS still allows nobody, and no secret is required to start. One difference: PORT="" now means 3000 (it gave 127.0.0.1: before), and a PORT outside 1-65535 stops the server at start.

Not done, on purpose

  • No zod: the reads are plain strings and numbers, and lib/env.ts is imported by the Edge middleware. The issue named zod.
  • LAKE_WRITER_MAX_BATCHES and PREVALENCE_* stay in their routes (one line each, already through positiveIntFromEnv) and are not checked at start.

Checks

  • tsc --noEmit clean; npm run lint 0 errors, 113 warnings (same as main); vitest run tests/unit 1499 passed, 1 skipped.
  • New tests: env.test.ts (accessors), platform-operator.test.ts (new file), tenant.test.ts (verifyProxyAuth), cron-auth.test.ts (empty secret), snapshot-publish.test.ts, session-lookup.test.ts (PORT), instrumentation.test.ts.
  • Not run: the integration suite (no Postgres up; admin-tenants sets PLATFORM_OPERATOR_USER_IDS), and a real Next start with a bad value.

Closes #47

🤖 Generated with Claude Code

@SherlockOmss

Copy link
Copy Markdown
Contributor Author

Pushed b36878d (first push). Files: lib/env.ts, lib/tenant.ts, lib/cron-auth.ts, lib/platform-operator.ts, lib/session-lookup.ts, lib/snapshot-publish.ts, app/api/cron/build-snapshots/route.ts, instrumentation-node.ts, docs/configuration.md, 7 unit test files. Local checks: tsc clean, lint 0 errors (113 warnings), unit 1499 passed. CI: pending at the time of writing.

…arams through lib/env (#47)

Add proxySecret, cronSecret, platformOperatorIds and listenPort to lib/env.ts and use them in
lib/tenant.ts, lib/cron-auth.ts, lib/platform-operator.ts and lib/session-lookup.ts. Move the
snapshot parameter builder to lib/snapshot-publish.ts as snapshotParamsFromEnv. Check PORT and
the SNAPSHOT_* numbers at start with AUTH_TRUSTED_ORIGINS. An unset or empty secret still
refuses every call.

Refs #47

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: SherlockOmss <[email protected]>
@SherlockOmss
SherlockOmss force-pushed the refactor/47-env-accessors branch from b36878d to 06226ca Compare November 25, 2026 13:59
@SherlockOmss

Copy link
Copy Markdown
Contributor Author

Pushed 06226ca: same commit as b36878d with its Signed-off-by added (the dco / dco check failed on the missing line). Tree identical, force-pushed with lease on a branch with no review yet. Files: none changed.

@SoFrexs SoFrexs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 06226ca.

The secrets keep failing closed. proxySecret() and cronSecret() return undefined for unset or empty, and the callers still refuse (500 for cron; verifyProxyAuth throws "not configured"). An empty or missing operator list still allows nobody. No new error message carries a secret: only PORT and SNAPSHOT_* values are echoed. The SNAPSHOT_* defaults and validation moved unchanged. Renaming the local to presentedSecret in verifyProxyAuth avoids shadowing the new import, and a listenPort throw inside lookupSession is caught and returns null, so that path still fails closed.

[should-fix] lib/env.ts:101: listenPort accepts values Next reads differently. It uses Number(raw), while the standalone server listens on parseInt(process.env.PORT, 10) || 3000. So PORT=0x1F90 passes the start check as 8080 while Next listens on 3000, and PORT=1e3 gives 1000 against 1. The middleware then calls the session route on the wrong port and every console request redirects to /login, which is the mistake a start-time check is there to catch. Accept only /^\d+$/, then the range check, and add "0x1F90" and "1e3" to the "throws on anything else" test.

[should-fix] A bad SNAPSHOT_* value now stops the whole server. Before, it failed only the build-snapshots job; now ingest goes down with it, even when SYNTHAEA_SNAPSHOT_SIGNER is unset and no snapshot is ever built. Failing at start is defensible and #47 asks for it, but name it under "Behavior" next to the PORT change so whoever deploys knows. If it isn't intended, check SNAPSHOT_* only when a signer is configured.

[should-fix] Closes #47 closes what this PR leaves out. #47 also asks to move the lake/prevalence parameter builders into their lib modules, and names zod. Skipping both is fine, but closing the issue loses the reasons: record them in #47, or use Refs #47 with a follow-up for PREVALENCE_*/LAKE_WRITER_MAX_BATCHES start-up validation.

[nit] lib/session-lookup.ts:13-14: two blank lines after the import.

[nit] paramsFromEnv's doc comment was dropped. "Provisional starting points, set from the environment until slice 6 calibrates them" is a why-comment; keep it on snapshotParamsFromEnv or DEFAULT_SNAPSHOT_PARAMS.

[nit] Tests set the environment two ways. The new verifyProxyAuth block in tenant.test.ts and the cron-auth.test.ts case save and restore process.env by hand; platform-operator.test.ts and session-lookup.test.ts use vi.stubEnv/vi.unstubAllEnvs. Prefer vi.stubEnv: process.env.X = saved with saved undefined stores the string "undefined", a latent cross-test leak.

Verified

  • Diff and #47 read; the only direct env reads left are NODE_ENV in lib/prisma.ts and a doc comment in cron-auth.ts.
  • tsc --noEmit clean; npm run lint 0 errors, 113 warnings (the ceiling); vitest run tests/unit 1487 passed, 5 skipped (Windows).
  • The Number vs parseInt difference, in node.
  • Not run: integration tests (no Docker here).

🤖 Generated with Claude Code

Comment thread lib/env.ts
/** The port the Next.js server listens on (`PORT`, 3000 when unset or empty). A value that is not 1-65535 throws. */
export function listenPort(env: Environment): number {
const raw = env.PORT;
if (raw === undefined || raw === "") return 3000;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] Number(raw) accepts 0x1F90 (8080) and 1e3 (1000), but the standalone server listens on parseInt(process.env.PORT, 10) || 3000 (3000 and 1). Accept /^\d+$/ only, and add both values to the "throws" test.

Comment thread lib/session-lookup.ts
*/
import { listenPort } from "@/lib/env";


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Double blank line after the import.

Jihair54 added a commit that referenced this pull request Nov 26, 2026
#53) (#100)

* refactor(storage): one module for the storage root and its layout (Refs #53)

Eight callers built `path.join(process.cwd(), "storage", ...)` inline.
lib/storage-paths.ts now names the root (storageRoot, storagePath)
and the layouts used more than once (artifactsDir, artifactFile,
corpusExportDir). keyRotationFile and releaseArtifactFile keep their
signatures and build on storagePath. Behaviour is unchanged: the root
is still storage/ under the working directory, read on each call, so
tests that chdir into a sandbox keep working.

Two callers had a local variable named like the new helper
(artifactsDir in the IOC feed route, storagePath in
loadManifestBytes); they are renamed, since the helper would
otherwise be read inside its own initializer.

Relocating the root (STORAGE_DIR, as #53 proposes) is left for a
follow-up through lib/env.ts once #97 lands, to avoid two PRs editing
the same accessors.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jihair54 <[email protected]>

* refactor(storage): drop three uncalled path builders, group the new imports

Review of #100: contentArtifactPath, contentManifestPath and
releaseManifestPath had no caller in app/, lib/, scripts/ or tests/,
and contentArtifactPath named the artifact layout a second time. They
are removed; the #804 rationale that sat on contentArtifactPath moves
to artifactFile. The storage-paths import joins the other @/lib imports
in the artifact route and corpus.ts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jihair54 <[email protected]>

---------

Signed-off-by: Jihair54 <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SV-014: Config read ad hoc, parsers copy-pasted, no validation at startup

2 participants